package com.huqing.icu.controller.app;

import com.huqing.icu.base.BaseController;
import com.huqing.icu.base.IdReq;
import com.huqing.icu.inout.req.app.CommunityPostAddReq;
import com.huqing.icu.inout.req.app.CommunityPostListQueryReq;
import com.huqing.icu.service.app.ICommunityPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 社区帖子接口
 * @Author huqing
 * @Date 2025/4/6 20:58
 **/
@RestController
@RequestMapping("/api/community_post")
@Tag(name = "社区帖子接口")
public class CommunityPostController extends BaseController {

    @Autowired
    private ICommunityPostService iCommunityPostService;

    @Operation(summary = "新增帖子")
    @RequestMapping(value = "/add_post", method = RequestMethod.POST, produces = "application/json")
    public String addPost(@RequestBody CommunityPostAddReq addReq, HttpServletRequest request) {
        //Long userId = checkAndGetUserId(request);
        Long userId = 9600L;
        addReq.setUserId(userId);
        return jsonSuccess(iCommunityPostService.addPost(addReq));
    }

    @Operation(summary = "新增帖子test")
    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
    public String test(HttpServletRequest request) {
        CommunityPostAddReq addReq = new CommunityPostAddReq();
        addReq.setUserId(9600L);
        addReq.setTitle("这是一个标题");
        addReq.setContent("这是内容");
        return jsonSuccess(iCommunityPostService.addPost(addReq));
    }

    @Operation(summary = "查询帖子列表")
    @RequestMapping(value = "/query_list", method = RequestMethod.POST, produces = "application/json")
    public String queryList(@RequestBody CommunityPostListQueryReq listQueryReq, HttpServletRequest request) {
        //Long userId = checkAndGetUserId(request);
        Long userId = null;
        return jsonSuccessForNull(iCommunityPostService.queryList(listQueryReq, userId));
    }

    @Operation(summary = "查询帖子详情")
    @RequestMapping(value = "/query_detail", method = RequestMethod.POST, produces = "application/json")
    public String queryList(@RequestBody IdReq idReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        return jsonSuccessForNull(iCommunityPostService.queryDetail(idReq.getId(), userId));
    }

    @Operation(summary = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    public String delete(@RequestBody IdReq idReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        Long id = idReq.getId();
        return jsonSuccess(iCommunityPostService.delete(id, userId));
    }

    @Operation(summary = "点赞")
    @RequestMapping(value = "/like", method = RequestMethod.POST, produces = "application/json")
    public String like(@RequestBody IdReq idReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        Long id = idReq.getId();
        return jsonSuccess(iCommunityPostService.like(id, userId));
    }

    @Operation(summary = "收藏")
    @RequestMapping(value = "/favorite", method = RequestMethod.POST, produces = "application/json")
    public String favorite(@RequestBody IdReq idReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        Long id = idReq.getId();
        return jsonSuccessForNull(iCommunityPostService.favorite(id, userId));
    }

}
